﻿<%@ control language="C#" autoeventwireup="true" inherits="UserControl_ManagerProduct, App_Web_pmcgjoc4" %>
<%@ Import Namespace="DacSanMienTayOBJ" %>

<%@ Import Namespace="System.Collections.Generic" %>
<script type="text/javascript" src="Scripts/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="ckeditor/adapters/jquery.js"></script>
<script language="javascript" src="Scripts/jquery.alphanumeric.js"></script>
<script type="text/javascript" src="<%=Utility.modHost %>/Scripts/jquery.simplemodal.js"></script>

 <style>
  .imgareaselect-border1 {
	background: url('../images/border-v.gif') repeat-y left top;
}

.imgareaselect-border2 {
    background: url(../images/border-anim-h.gif) repeat-x left top;
}

.imgareaselect-border3 {
    background: url(../images/border-anim-v.gif) repeat-y right top;
}

.imgareaselect-border4 {
    background: url(../images/border-anim-h.gif) repeat-x left bottom;
}

.imgareaselect-border1, .imgareaselect-border2,
.imgareaselect-border3, .imgareaselect-border4 {
    filter: alpha(opacity=50);
	opacity: 0.5;
}

.imgareaselect-handle {
    background-color: #fff;
	border: solid 1px #000;
    filter: alpha(opacity=50);
	opacity: 0.5;
}

.imgareaselect-outer {
	background-color: #000;
    filter: alpha(opacity=50);
	opacity: 0.5;
}

.imgareaselect-selection {
}

#simplemodal-overlay {background-color:#000;}
#simplemodal-container {height:378px; width:619px; color:#bbb; background:url(../Images/bg_login.png) no-repeat center; padding:12px; position:relative;}
#simplemodal-container .simplemodal-data {position:relative;}
#simplemodal-container a.modalCloseImg {background:url(../Images/x.png) no-repeat; width:25px; height:29px; display:inline; z-index:3200; position:absolute; top:0px; right:0px; cursor:pointer;}

#simplemodal_container_uploadimage {height:490px; width:700px; color:#bbb;background-color:White;border:5px;border-style:solid;border-color:#ce5c00; position:relative;margin-top:50px}
#simplemodal_container_uploadimage .simplemodal-data {position:relative;}
#simplemodal_container_uploadimage a.modalCloseImg {background:url(../Images/x.png) no-repeat; width:25px; height:29px; display:inline; z-index:3200; position:absolute; top:-16px; right:-16px; cursor:pointer;}


  
  </style>

<div>
<input type="button" id="cmdShowAndHideProduct" value="Thêm mới" />
</div>

<div id="divProductInfo" style="display:none">
<table id="tbProductInfo">

<tr>
<td style="width:100px">
Tên sản phẩm
</td>
<td class="style1">
<input type="text" id="txtProductName" name="txtProductName"  style="width:300px" onfocus="ResetFieldProduct(1)" />
<label id="lbErrorProductName" style="color:red"></label>
</td>
</tr>

<tr>
<td style="width:100px">
Giá gốc
</td>
<td class="style1">
<input type="text" id="txtOriginalPrice" name="txtOriginalPrice" class="sample4"  style="width:300px" onfocus="ResetFieldProduct(2)" />
<label id="lbErrorOriginalPrice" style="color:red"></label>
</td>
</tr>

<tr>
<td style="width:100px">
Giá bán
</td>
<td class="style1">
<input type="text" id="txtRealPrice" name="txtRealPrice"  class="sample4" style="width:300px" onfocus="ResetFieldProduct(3)" />
<label id="lbErrorRealPrice" style="color:red"></label>
</td>
</tr>


<tr>
<td style="width:100px">
Số lượng
</td>
<td class="style1">
<input type="text" id="txtQuantity" name="txtQuantity" class="sample4" style="width:300px" onfocus="ResetFieldProduct(4)" />
<label id="lbErrorQuantity" style="color:red"></label>
</td>
</tr>

<tr>
<td style="width:100px">
Đơn vị tính
</td>
<td class="style1">
<input type="text" id="txtMeasurement" name="txtMeasurement"  style="width:300px" onfocus="ResetFieldProduct(5)" />
<label id="lbErrorMeasurement" style="color:red"></label>
</td>
</tr>

<tr>
<td style="width:100px">
Chọn vùng miền
</td>
<td class="style1">
<select id="cbProvince" name="cbProvince" onchange="ShowDistrictFollowProvince(this.value)">
<option value="0" >Chọn vùng miền</option>
<%
    try
    {
        List<Province> listProvince = DistrictBusiness.ListAllProvince(1, ConnectionType.AdminConnection);
        foreach (Province province in listProvince)
        {
        %>
            <option value="<%=province.ProvinceID %>"><%=province.ProvinceName%></option>
            <%
    }
    }
    catch
    {

    }
    
     %>
     </select>
     
</td>
</tr>

<tr id="trDistrict">
<td style="width:100px">
Chọn quận/huyện
</td>
<td class="style1">
<select id="cbDistrict" name="cbDistrict" onchange="ResetFieldProduct(6)">

</select>
    <label id="lbErrorDistrict" style="color:red"></label>
</td>
</tr>

<tr>
<td style="width:100px">
Nguồn gốc sản phẩm
</td>
<td class="style1">
<textarea id="txtProductOriginal" name="txtProductOriginal" cols="1" rows="3" style="width:800px" ></textarea> 
</td>
</tr>

<tr>
<td style="width:100px">
Hướng dẫn sử dụng
</td>
<td class="style1" >
<textarea id="txtProductGuide" name="txtProductGuide" cols="1" rows="3" style="width:800px" ></textarea> 

</td>
</tr>

<tr>
<td style="width:100px">
Từ khóa
</td>
<td class="style1" >
<textarea id="txtMetakey" name="txtMetakey" cols="1" rows="3" style="width:800px" ></textarea> 

</td>
</tr>


<tr>
<td style="width:100px">
Hình ảnh
</td>
<td class="style1">
<image src="" height="100px" width="100px" id="imgProduct" />
<asp:FileUpload ID="fileImage" runat="server" />

</td>
</tr>

<tr id="trDescription">
<td style="width:100px">
Nội dung
</td>
<td class="style1" >
<textarea style="width: 700px;margin-left:60px" id="txtDescription" rows="2" cols="1"  name="txtDescription"  ><span id="spanDescription"></span></textarea>
 <input name="txt_descPro"  id="txt_descPro" type="hidden" value="hello"/>
</td>
</tr>


<tr>
<td colspan="2">
<asp:Button ID="cmdAddProduct" runat="server" Text="Tạo mới" 
        OnClientClick="return CheckValidProduct()" onclick="cmdAddProduct_Click" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Button ID="cmdUpdateProduct" runat="server" Text="Cập nhật lại" 
        OnClientClick="return CheckValidProductUpdate()" 
        onclick="cmdUpdateProduct_Click" />
       
</td>

</tr>
</table>

</div>

<div>
<span>
Danh Sách Sản Phẩm

</span>

<table>
<tr>
<td style="width:250px">
Tên sản phẩm
</td>
<td style="width:100px">
Hình ảnh
</td>

<td style="width:100px">
Giá bán
</td>

<td style="width:120px">
Số lượng
</td>
<td style="width:120px">
Đơn vị tính
</td>
<td style="width:150px">
Chỉnh sữa
</td>

<td style="width:150px">
Khóa/mở khóa
</td>
</tr>
<tbody id="bodyProductList">
<%
    try
    {
        int recore = 250;
        int stt = 1;
        List<Product> listProduct = ProductBusiness.ListAllProduct(0, recore, ConnectionType.AdminConnection);
        if (listProduct.Count > 0)
        {
            foreach (Product product in listProduct)
            {

                string productName = product.ProductName;
                string imagePath = Utility.modHost + "/" + product.ProductImage;
                bool checkLocked = false;
               
               
                string statusAdv = "";
                if (product.IsVisble == 0)
                {
                    statusAdv = "Đang bị khóa";
                    checkLocked = false;
                }
                else if (product.IsVisble == 1)
                {
                    statusAdv = "Đang active";
                    checkLocked = true;
                }
                string description = product.ContentProduct;
                if (description != "")
                {
                  //  description = HttpUtility.HtmlDecode(description);
                }
                string realPrice="0";
                try
                {
                    realPrice = string.Format("{0:0,0},VND",product.RealPrice);
                }
                catch
                {

                }
                int quantity = 0;
                try
                {
                    quantity = product.Quantity;
                }
                catch
                {

                }
                
                string linkProduct = Utility.userHost + "/san-pham/" + product.ProductID +"-"+Utility.sefWord(product.ProductName)+ ".html";
              
        %>
        <tr id="trProduct<%=stt %>">
        <td>
        <a href="<%=linkProduct %>" target="_blank" style="text-decoration:none">
        <%=productName%>
        </a>
        </td>
    
        <td>
         <a href="<%=linkProduct %>" target="_blank" style="text-decoration:none">
       <image src="<%=imagePath %>" height="100px" width="100px" />
       </a>
        </td>
        <td>
        <%=realPrice%>
        </td>
        <td>
        <%=quantity%>
        </td>
         <td>
        <%=product.MeasurementUnits%>
        </td>
    
       <td>
 
       <a href="#" style="color:Blue; cursor:pointer" title="<%=productName %>"  onclick="ShowInfoProduct('<%=product.ProductID %>',this.title,'<%=product.OriginalPrice %>','<%=product.RealPrice %>','<%=product.Quantity %>','<%=product.MeasurementUnits%>','<%=product.ProductOriginal %>','<%=product.ProductGuide %>','<%=imagePath %>','<%=product.ProductImage %>','<%=product.Metakeyword %>')"> Chỉnh sữa</a>
       </td>
       <td>
        <%
            if (checkLocked)
            {
                 %>
                    <input type="button" title="<%=product.ProductID %>"  value="Khóa" onclick="LockAndUnlockProduct(this.title,'lock','<%=stt %>')" />
                <%
            }
            else
            {
             %>
                 <input type="button" title="<%=product.ProductID %>"  value="Mở khóa" onclick="LockAndUnlockProduct(this.title,'unlock','<%=stt %>')" />
                <%
            }
                     %>
       </td>
        <td>
       <span title="<%=product.ProductID %>" style="color:blue;cursor:pointer" onclick="ShowListSelectOptionProduct(this.title)" >Chọn option</span>
       </td>
       <td>

       <span style="cursor:pointer; color:blue" onclick="MoveArticle('<%=product.ProductID %>','movetop')">Top</span>
       <span style="cursor:pointer; color:blue" onclick="MoveArticle('<%=product.ProductID %>','movebottom')">bottom</span>
       </td>
        </tr>
        <%
            stt++;
    }
            
          %>
          <tr style="line-height:30px">
          <td>
          
          </td>
          </tr>
         <tr>
          <td colspan="7">
          Trang : 
         <%
             
             int totalRecore = ProductBusiness.CountAllProduct(ConnectionType.AdminConnection);
             int numPage = totalRecore / recore;
             int balance = totalRecore % recore;
             if (balance > 0)
             {
                 numPage += 1;
             }
             for (int i = 1; i <= numPage; i++)
             {
             %>
         <a href="#" style="text-decoration:none" onclick="PaggingProductList('<%=i %>')"><%=i %> &nbsp</a>

         <%
            }
                %>
         </td>

         </tr>
          
          <%  
        }
    }
    catch
    {

    }
     %>

</tbody>

</table>
</div>

 <div id="divListProductMap" style="display:none; background-color:White; height:360px; width:720px; ">
<div style="height:30px; background-color:#eecccc">
<div style="width:500px; top:5px; position:relative">
<span style="color:red; font-weight:bold;  ">Chọn danh mục, sản phẩm liên quan</span>
</div>

</div>
       <div id="divListProductSelect" style="overflow-y:auto; height:440px; width:650px;">
       <table>
<span style="color:black">Chọn danh mục</span>
<tr style="line-height:50px">

<td style="width:200px">

<select style="width:200px">
<option> Chon danh muc san pham </option>
</select>
</td>

<td style="width:200px">


<select style="width:200px">
<option> Chon danh muc san pham </option>
</select>
</td>
<td style="width:200px">


<select style="width:200px">
<option> Chon danh muc san pham </option>
</select>
</td>
<input type="button" value="Cap nhat" style="position:relative; top:70px; left:440px" />
</tr>
<tr style="height:80px"></tr>
<span style="color:black; position:relative; top:130px; left:-160px">Chọn nhóm sản phẩm</span>
<tr>
<td style="width:200px">

<select style="width:200px">
<option> Chon danh muc san pham </option>
</select>
</td>

<td style="width:200px">


<select style="width:200px">
<option> Chon danh muc san pham </option>
</select>
</td>
<td style="width:200px">


<select style="width:200px">
<option> Chon danh muc san pham </option>
</select>
</td>
<input type="button" value="Cập nhật" style="position:relative; top:190px; left:235px" />
</tr>

<tr style="height:80px"></tr>
<span style="color:black; position:relative; top:240px; left:-370px">Chon hình ảnh</span>
<tr>

<td style="width:200px">

<select style="width:200px">
<option> Chon danh muc san pham </option>
</select>
</td>

<td style="width:200px">


<select style="width:200px">
<option> Chon danh muc san pham </option>
</select>
</td>
<td style="width:200px">


<select style="width:200px">
<option> Chon danh muc san pham </option>
</select>
</td>
</tr>
      
       </table>
       </div>
       <div style="height:35px; background:#cccccc">
<input type="hidden" id="hdProductIDSelect" />
<div style="position:relative; left:500px; width:120px; top:5px ">
<%--<input type="button" value="Xác nhận" style="left:-80px; position:relative; top:2px" onclick="ConfirmRestaurantMap()" />--%>
<input type="button" value="Đóng lại" style="left:100px; position:relative; top:2px" onclick="ClosePopupRes()" />
</div>
</div>
</div>


  <div id="div_topinfor" class="simplemodal-data" style="display:none"></div>
<input type="hidden" id="hdProductID" name="hdProductID" />
<input type="hidden" id="hdProductImage" name="hdProductImage" />

<script type="text/javascript">


    $('#cmdShowAndHideProduct').click(function () {
        var valueButton = $('#cmdShowAndHideProduct').val();
        if (valueButton == 'Thêm mới') {
            $('#divProductInfo').css("display", "inline");
            $('#cmdShowAndHideProduct').val('Đóng lại');
        }
        else if (valueButton == 'Đóng lại') {
            $('#divProductInfo').css("display", "none");
            $('#cmdShowAndHideProduct').val('Thêm mới');

        }

    });

    $('.sample4').numeric();


    $(document).ready(function () {


        CKEDITOR.on('dialogDefinition', function (ev) {
            var dialogName = ev.data.name;
            var dialogDefinition = ev.data.definition;
            if (dialogName == 'link') {
                dialogDefinition.removeContents('advanced');
                dialogDefinition.removeContents('target');
            }
            if (dialogName == 'image') {
                dialogDefinition.removeContents('advanced');
                dialogDefinition.removeContents('Link');
            }
            if (dialogName == 'flash') {
                dialogDefinition.removeContents('advanced');
            }
        });
        CKEDITOR.replace('txtDescription',
            {
                fullPage: true,
                extraPlugins: 'tableresize,docprops,autogrow,uploadimage',
                autoGrow_maxHeight: 250,
                toolbar:
	            [
		            { name: 'styles', items: ['Font', 'FontSize'] },
	                { name: 'basicstyles', items: ['Bold', 'Italic', 'Underline', '-', 'TextColor', 'BGColor'] },
	                { name: 'paragraph', items: ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock'] },
	                { name: 'insert', items: ['Uploadimage', '-', 'Table', 'Smiley'] },
                   { name: 'links', items: ['Link', 'Unlink', 'Anchor'] },
	            ]
            });


        });


        function InsertImageContent() {

            ShowUploadImageContent();
        }

        function ShowUploadImageContent() {
            var rand_no = Math.random();
            var loading = '<img src="<%=Utility.modHost %>/Icons/loading.gif" alt="" style="margin-left:auto;margin-right:auto;margin-top:23%;margin-left:45%;width:40px;height:40px" />';
            $('#div_topinfor').html(loading);
            $('#div_topinfor').modal({
                opacity: 80,
                containerId: 'simplemodal_container_uploadimage',
                onOpen: function (dialog) {
                    dialog.overlay.fadeIn('slow', function () {
                        dialog.container.slideDown('slow', function () {
                            dialog.data.fadeIn('slow');
                            $('#div_topinfor').load("<%=Utility.modHost %>/Proccess/ProccessShowButtonUploadProduct.aspx?_=" + rand_no);
                        });
                    });
                },
                onClose: function (dialog) {
                    dialog.data.fadeOut('slow', function () {
                        dialog.container.slideUp('slow', function () {
                            dialog.overlay.fadeOut('slow', function () {
                                $.modal.close();
                                $('#div_topinfor').html('');
                            });
                        });
                    });
                }
            });
        }


        function CheckValidProduct() {
            var productName = $('#txtProductName').val();
            var originalPrice = $('#txtOriginalPrice').val();
            var realPrice = $('#txtRealPrice').val();
            var quantity = $('#txtQuantity').val();
            var measurement = $('#txtMeasurement').val();
            var checkRegullar = true;
            if (productName == '') {
                checkRegullar = false;
                $('#lbErrorProductName').text("*");
            }
            var districtID = 0;
            districtID = $('#cbDistrict').val();
            if (originalPrice == '') {
                checkRegullar = false;
                $('#lbErrorOriginalPrice').text("*");
            }
            if (realPrice == '') {
                checkRegullar = false;
                $('#lbErrorRealPrice').text("*");
            }
            if (quantity == '') {
                checkRegullar = false;
                $('#lbErrorQuantity').text("*");
            }
            if (measurement == '') {
                checkRegullar = false;
                $('#lbErrorMeasurement').text("*");
            }
            if (districtID == '0') {
                checkRegullar = false;
                $('#lbErrorDistrict').text("*");
            }

            if (checkRegullar == false) {
                return false;
            }
            else {
                return true;
            }
        }

        function ResetFieldProduct(id) {
            if (id == '1') {
                $('#lbErrorProductName').text(" ");
            }
            if (id == '2') {
                $('#lbErrorOriginalPrice').text(" ");
            }
            if (id == '3') {
                $('#lbErrorRealPrice').text(" ");
            }
            if (id == '4') {
                $('#lbErrorQuantity').text(" ");
            }
            if (id == '5') {
                $('#lbErrorMeasurement').text(" ");
            }
            if (id == '6') {
                $('#lbErrorDistrict').text(" ");
            }
        }

        function ShowInfoProduct(productID, productName,originalPrice,realPrice,quantity,measurement,productOriginal,productGuide,productImage,image,metakey) {

            CKEDITOR.instances.txtDescription.html='';

            $('#hdProductID').val(productID);
            $('#hdProductImage').val(image);
            $('#divProductInfo').css("display", "inline");
            $('#txtProductName').val(productName);

            $('#txtOriginalPrice').val(originalPrice);
            $('#txtRealPrice').val(realPrice);

            $('#txtQuantity').val(quantity);
            $('#txtMeasurement').val(measurement);

            $('#txtProductOriginal').val(productOriginal);
            $('#txtProductGuide').val(productGuide);
            $('#txtMetakey').val(metakey);
            $('#imgProduct').attr("src", productImage);
            $.post("<%=Utility.modHost %>/Proccess/ProccessGetDescriptionProduct.aspx", { productID: productID }, function (data) {

                var oEditor = CKEDITOR.instances.txtDescription;
                oEditor.setData(data);
                

            });

            $('body,html').animate({ scrollTop: 0 }, 300);
        }

        function LockAndUnlockProduct(productID, type, vt) {
            var trID = '#trArticle' + vt;
            var load = '<img src="<%=Utility.modHost %>/Images/loading.gif" id="imgLoad" alt="" style="margin-left:auto;margin-right:auto;margin-top:53%;margin-left:135%;width:40px;height:40px" />';
            $(trID).html(load);

            $.post("<%=Utility.modHost %>/Proccess/ProccessLockAndUnlockProduct.aspx", { productID: productID, type: type, vt: vt }, function (data) {
                $('#imgLoad').attr("src", "");
                if (data == "lockfaile") {
                    alert('Không khóa được sản phẩm này. Vui lòng liên hệ admin');
                }
                else if (data == "unlockfaile") {
                    alert('Không mở khóa được sản phẩm này. Vui lòng liên hệ admin');
                }
                else {
                    document.getElementById('trProduct' + vt).innerHTML = data;
                }
            });
        }

        function CheckValidProductUpdate() {
            var productName = $('#txtProductName').val();
            var originalPrice = $('#txtOriginalPrice').val();
            var realPrice = $('#txtRealPrice').val();
            var quantity = $('#txtQuantity').val();
            var measurement = $('#txtMeasurement').val();
            var checkRegullar = true;
            if (productName == '') {
                checkRegullar = false;
                $('#lbErrorProductName').text("*");
            }
            if (originalPrice == '') {
                checkRegullar = false;
                $('#lbErrorOriginalPrice').text("*");
            }
            if (realPrice == '') {
                checkRegullar = false;
                $('#lbErrorRealPrice').text("*");
            }
            if (quantity == '') {
                checkRegullar = false;
                $('#lbErrorQuantity').text("*");
            }
            if (measurement == '') {
                checkRegullar = false;
                $('#lbErrorMeasurement').text("*");
            }

            if (checkRegullar == false) {
                return false;
            }
            else {
                return true;
            }
        }

        function ShowDistrictFollowProvince(idProvince) {
           // $('#trDistrict').css("display", "block");
            $.post("<%=Utility.modHost %>/Proccess/ProccessGetListDistrictFollowProvince.aspx", { idProvince: idProvince }, function (data) {

                document.getElementById('cbDistrict').innerHTML = data;

            });
        }

        function ShowListSelectOptionProduct(productID) {
            var heightScreen = window.screen.height;
            var heightModal = heightScreen - 385;
            var heightDiv = heightScreen - 425;
            var heightTable = heightScreen - 485;
            $('#divListProductMap').attr("display", "inline");
            $('#divListProductMap').modal();
            $('#divListProductMap').height(heightDiv);
            $('#divListProductSelect').height(heightTable);
            $('#hdProductIDSelect').val(productID);
            $('#simplemodal-container').css("top", "20px");
            $('#simplemodal-container').height(heightModal);
            $('#simplemodal-container').width(730);
            var load = '<img src="<%=Utility.modHost %>/Images/loading.gif" id="imgLoad" alt="" style="margin-left:auto;margin-right:auto;margin-top:123%;margin-left:85%;width:40px;height:40px" />';
            //$('#bodyListProductSelect').html(load);
            $.post("<%=Utility.modHost %>/Proccess/ProccessShowListOptionSelectProduct.aspx", { productID: productID }, function (data) {

                document.getElementById('bodyListProductSelect').innerHTML = data;

            });
        }

        function ClosePopupRes() {
            $.modal.close();
        }

    </script>